/**
* @Author: Zhengfeng.Yao
* @Date:   2017-01-23 14:12:23
* @Last modified by:   Zhengfeng.Yao
* @Last modified time: 2017-02-10 11:26:46
*/

import fetchJSONP from 'fetch-jsonp';

const domain = window.proxyDomain || '';
const eulerDomain = window.eulerDomain || domain;
const oceanusDomain = window.oceanusDomain || domain;

const _csrf = { [$('#_csrf').attr('name')]: $('#_csrf').val() }
// 通用指标搜索
export async function searchIndicatrix(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  const params = Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&');
  return await fetch(`${domain}/generalIndex/getGeneralIndex?${params}`, {
      credentials: 'include'
    })
    .then(res => res.json());
}
// 修改指标状态
export async function updataIndicatrixState(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  const params = Object.keys(data).map(key => `${key}=${data[key]}`).join('&');
  return await fetch(`${domain}/generalIndex/${querys.status == 0?'disableIndex':'enableIndex'}?${params}`, {
      credentials: 'include',
      method: 'post'
    })
    .then(res => res.json());
}
// 添加通用指标
export async function addBasic(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/generalIndex/addBasic`, {
      credentials: 'include',
      method: 'post',
      body: formData
    })
    .then(res => res.json());
}
// 添加衍生指标
export async function addExtended(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/generalIndex/addExtended`, {
      credentials: 'include',
      method: 'post',
      body: formData
    })
    .then(res => res.json());
}

//合作方指标 列表
export async function queryPartnerIndicatores(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/partnerAllocation/queryAllocations.json`, {
    credentials: 'include',
    method: 'post',
    body: formData
  })
    .then(res => res.json());
}


//查询合作方的指标分配状态
export async function queryPartnerConfigState(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/partnerAllocation/queryAllocationState.json`, {
    credentials: 'include',
    method: 'post',
    body: formData
  })
    .then(res => res.json());
}

//全部配置、全部取消配置
export async function configAllPartnerIndicatrixes(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/partnerAllocation/configBatchPartnerIndicatrixes.json`, {
    credentials: 'include',
    method: 'post',
    body: formData
  })
    .then(res => res.json());
}

//配置、取消配置
export async function configPartnerIndicatrixes(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/partnerAllocation/configPartnerIndicatrixes.json`, {
    credentials: 'include',
    method: 'post',
    body: formData
  })
    .then(res => res.json());
}

//指标日志
export async function queryLoggerList(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  const params = Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&');
  return await fetch(`${domain}/callLog/findCallLog?${params}`, {
    credentials: 'include',
    method: 'get'
  })
    .then(res => res.json());
}

//查询当前用户持有的合作方
export async function queryPartnerList(querys) {
  const data = Object.assign({}, _csrf, querys||{});
  let formData = new FormData();
  for(var key in data){
      formData.append(key, data[key]);
  }
  return await fetch(`${domain}/partner/queryHoldPartners.json`, {
    credentials: 'include',
    method: 'post',
    body: formData
  })
    .then(res => res.json());
}